// 购物车相关处理逻辑
import { setForEverData, getForEverData } from '../common/env';

const CART_DATA_PREFIX = 'cart_data_';

/**
 * 根据活动添加购物车，保存购物车数据
 * @param {Object} productInfo 选中的商品信息
 * @param {Number|String} activityId 活动id
 * @param {Number} addCount 添加数量，不传默认添加选中的商品的数量
 */
export const cartAddItem = (productInfo, activityId, addCount = 0) => {
    const cartList = getCartProductList(activityId);
    let result = [];
    const isInCart = cartList.filter(item => isSimpleProduct(item, productInfo)).length > 0;
    if (isInCart) {
        result = cartList.map(item => {
            if (isSimpleProduct(item, productInfo)) {
                return {
                    ...item,
                    count: addCount ? item.count + addCount : item.count + productInfo.count,
                };
            }
            return item;
        }); 
    } else {
        result = [...cartList, productInfo];
    }
    setForEverData(`${CART_DATA_PREFIX}${activityId}`, result);
};

/**
 * 删除购物车项
 * @param {Object} productInfo 商品信息
 * @param {Number|String} activityId 活动id
 */
export const cartMinusItem = (productInfo, activityId, minusCount = 0 ) => {
    const cartList = getCartProductList(activityId);
    let result = [];
    for (let i = 0; i < cartList.length; i++) {
        const item = cartList[i];
        if (isSimpleProduct(item, productInfo)) {
            item.count > 1 && result.push({
                ...item,
                count: minusCount ? item.count - minusCount : item.count - 1,
            });
        } else {
            result.push(item);
        }
    }
    setForEverData(`${CART_DATA_PREFIX}${activityId}`, result);
};

/**
 * 更改购物车项
 * @param {Object} productInfo 选中的商品信息
 * @param {Number|String} activityId 活动id
 * @param {Number} changeCount 更改数量，不传默认更改选中的商品的数量
 */
export const cartChangeItem = (productInfo, activityId, changeCount = 0) => {
    const cartList = getCartProductList(activityId);
    let result = [];
    const isInCart = cartList.filter(item => isSimpleProduct(item, productInfo)).length > 0;
    if (isInCart) {
        result = cartList.map(item => {
            if (isSimpleProduct(item, productInfo)) {
                return {
                    ...item,
                    count: changeCount ? changeCount : productInfo.count,
                };
            }
            return item;
        }); 
    } else {
        result = [...cartList, productInfo];
    }
    result = result.filter(item => item.count > 0);
    setForEverData(`${CART_DATA_PREFIX}${activityId}`, result);
};

// 判断是否是同一个商品
const isSimpleProduct = (one, two) => {
    return one.productId == two.productId;
    // return one.productId == two.productId && one.selectSku.activitySkuId == two.selectSku.activitySkuId;
};

/**
 * 根据活动获取相应的购物车数据
 * @param {Number|String} activityId 活动id
 */
export const getCartProductList = (activityId) => {
    const list = getForEverData(`${CART_DATA_PREFIX}${activityId}`);
    return list || [];
};

/**
 * 根据活动获取相应的购物车价格、数量
 * @param {Number|String} activityId 活动id 
 */
export const getCartProductTotal = (activityId) => {
    const list = getForEverData(`${CART_DATA_PREFIX}${activityId}`);
    let totalPrice = 0;
    let totalAmount = 0;
    for (let i = 0; i < list.length; i++) {
        const item = list[i]; 
        totalPrice += item.count * item.selectSku.price;
        totalAmount += parseInt(item.count);
    }
    return { totalPrice, totalAmount };
};

/**
 * 清空购物车
 * @param {Number|String} activityId 活动id
 */
export const clearCartData = (activityId) => {
    setForEverData(`${CART_DATA_PREFIX}${activityId}`, []);
};